/**
 * Created by Administrator on 2018/6/4 0004.
 */
const AuthInfo = require('../model/authInfo.model')

const {handleError, handleSuccess} = require('../units/handle')
class AuthInfoController {
    static async setUserInfo(ctx, next) {
        const {user_id} = ctx.request.body
        const isAdd = await AuthInfo.findOne({user_id: user_id}).catch(e => {
            console.log(e)
        })
        console.log(isAdd)
        if (isAdd) {
            handleError({ctx, message: "已经设置用户信息"})
        } else {
            const data = await AuthInfo.create(ctx.request.body).catch(e => {
                console.log(e)
            })

            if (data) {
                handleSuccess({ctx, message: "设置成功"})
            } else {
                handleError({ctx, message: "设置失败"})
            }
        }

    }

    static async getUserInfo(ctx) {
        console.log(ctx.query)
        const {user_id} = ctx.query;
        const data = await AuthInfo.findOne({user_id: user_id}).catch(e => {
            console.log(e)
        })
        if (data) {
            handleSuccess({ctx, data: data})
        } else {
            handleError({ctx, message: "用户信息不存在"})
        }
    }

    static async updateInfo(ctx) {
        const {user_id} = ctx.request.body;
        const data = await AuthInfo.update({user_id}, ctx.request.body).catch(e => {
            console.log(e)
        })
        if (data) {
            handleSuccess({ctx, message: "更新成功"})
        } else {
            handleError({ctx, message: "更新失败"})
        }

    }
}
module.exports = AuthInfoController